package com.pengtu.gsj.service.impl.yh;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.yh.MainTenanceDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.MainTenance;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.DownService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.service.yh.MainTenanceService;
import com.pengtu.gsj.utils.MathsUtils;

@Transactional
@Service
public class MainTenanceServiceImpl implements MainTenanceService{
	
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private DownService downService;
	
	@Autowired
	private MainTenanceDao mainTenanceDao;

	@Override
	public MainTenance getMainTenanceById(String id) {
		return mainTenanceDao.get(id);
	}

	@Override
	public void saveMainTenance(MainTenance mainTenance) {
		mainTenanceDao.save(mainTenance);
	}

	@Override
	public void deleteMainTenance(MainTenance mainTenance) {
		mainTenanceDao.delete(mainTenance);
	}

	@Override
	public Page<MainTenance> findMainTenance(Page<MainTenance> page,
			QueryFilter queryFilter) {
		return mainTenanceDao.findPage(page, queryFilter);
	}

	@Override
	public List<MainTenance> getAllMainTenances() {
		return mainTenanceDao.getAll();
	}

	@Override
	public List<MainTenance> findMainTenances(QueryFilter queryFilter) {
		return mainTenanceDao.find(queryFilter);
	}

	@Override
	public Map<String, Object> getCheckContent(String startTime,
			String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Attachment> result = new ArrayList<Attachment>();
		List<Object> values = new ArrayList<Object>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		String maintenceId = (String)jsonObject.get("mainTenanceId");
		
		result = systemManageService.getCheckAttachments(startTime, endTime, tag);
		//根据orgId和时间 、标签查询是否有无附件 如果有 再将相关的附件加入到list中去
//		List<Attachment> list = systemManageService.getCheckAttachments(startTime, endTime, tag, maintenceId);
		double total = 0; //检查得分
		double percentage = 0; //系统完善度
		double tableFull = yearFull*4;
		List<String> resultList = Arrays.asList("0", "0", "0", "0", "0");
		if (StringUtils.isNotEmpty(maintenceId)) {
			MainTenance mainTenance = getMainTenanceById(maintenceId);
			if (mainTenance == null) {
				map.put("electronization", false);
			} else {
				total = yearFull; //此处是扣分  故用总分去相减
				for (Attachment attachment : mainTenance.getAttachments()) {
					if ("gcgl_jhap".equals(attachment.getTag())) {
						resultList.set(0, "1");
					}
					if ("gcgl_sjsc".equals(attachment.getTag())) {
						resultList.set(1, "1");
					}
					if ("gcgl_gcztb".equals(attachment.getTag())) {
						resultList.set(2, "1");
					}
					if ("gcgl_gcys".equals(attachment.getTag())) {
						resultList.set(3, "1");
					}
					result.add(attachment);
				}
				for (int i = 0; i < resultList.size()-1; i++) {
					if ("0".equals(resultList.get(i))) {
						total = MathsUtils.subDouble(total, yearPart);
					}
				}
				total = total < 0 ? 0 : total;
				percentage = MathsUtils.divDouble(total*100, yearFull, 2);
				total = 3*yearFull + total;
				resultList.set(4, String.valueOf(total));
				map.put("electronization", true);
				map.put("name", mainTenance.getProjectName());
				values.add(maintenceId);
			}
		} else {
			//判断是否有无检查项目
			map.put("electronization", false);
		}
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", result);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		map.put("resultList", resultList);
		map.put("percentage", percentage);
		return map;
	}

}
